var NormalCMS = angular.module("NormalCMS")

NormalCMS.controller('sysRoleCtrl', function ($scope, $http) {
    $('#table').bootstrapTable('destroy').bootstrapTable();
    $scope.init = function () {
        $scope.loadTable()
    }


    $scope.disableNode = function (nodes) {
        for (var j in nodes.nodes) {
            var state = nodes.nodes[j].state
            if (state) {
                nodes.nodes[j].state.disabled = true
            } else {
                nodes.nodes[j].state = {}
                nodes.nodes[j].state.disabled = true
            }

            for (var k in nodes.nodes[j].nodes) {
                var state2 = nodes.nodes[j].nodes[k].state
                if (state2) {
                    nodes.nodes[j].nodes[k].state.disabled = true
                } else {
                    nodes.nodes[j].nodes[k].state = {}
                    nodes.nodes[j].nodes[k].state.disabled = true
                }
            }
        }
    }

    $scope.showAuthList = function (row) {
        var role = row.power
        if (!role) {
            role = ""
        }
        role = role.split(',')
        $('#roleSetAuth').modal()
        var authTree = $('#authTree')
        authTree.children().remove()
        console.log(role)
        // var role = ['user', 'user-add', 'user-edit', 'home', 'home-find', 'project', 'project-gq', 'project-gq-add']

        var Auth = getAuths()

        for (var i in Auth) {
            if (role.indexOf(Auth[i].field) != -1) {
                if (Auth[i].state) {
                    Auth[i].state.checked = true
                } else {
                    Auth[i].state = {}
                    Auth[i].state.checked = true
                }
                for (var j in Auth[i].nodes) {
                    if (role.indexOf(Auth[i].nodes[j].field) != -1) {
                        if (Auth[i].nodes[j].state) {
                            Auth[i].nodes[j].state.checked = true
                        } else {
                            Auth[i].nodes[j].state = {}
                            Auth[i].nodes[j].state.checked = true
                        }
                        for (var k in Auth[i].nodes[j].nodes) {
                            if (role.indexOf(Auth[i].nodes[j].nodes[k].field) != -1) {
                                if (Auth[i].nodes[j].nodes[k].state) {
                                    Auth[i].nodes[j].nodes[k].state.checked = true
                                } else {
                                    Auth[i].nodes[j].nodes[k].state = {}
                                    Auth[i].nodes[j].nodes[k].state.checked = true
                                }
                            }
                        }
                    } else {
                        $scope.disableNode(Auth[i].nodes[j])
                    }
                }
            } else {
                $scope.disableNode(Auth[i])
            }
        }

        authTree.treeview({
            data: Auth,         // data is not optional
            levels: 5,
            showCheckbox: true,
            onNodeChecked: function (event, data) {
                //选中所有下级节点
                var childNodes = []
                for (var i in data.nodes) {
                    childNodes.push(data.nodes[i].nodeId)
                    for (var j in data.nodes[i].nodes) {
                        childNodes.push(data.nodes[i].nodes[j].nodeId)
                    }
                }
                console.log(childNodes)
                authTree.treeview('enableNode', [childNodes, { silent: true }]);
                authTree.treeview('checkNode', [childNodes, { silent: true }]);

            },
            onNodeUnchecked: function (event, data) {
                var childNodes = []
                for (var i in data.nodes) {
                    childNodes.push(data.nodes[i].nodeId)
                    for (var j in data.nodes[i].nodes) {
                        childNodes.push(data.nodes[i].nodes[j].nodeId)
                    }
                }
                authTree.treeview('uncheckNode', [childNodes, { silent: true }]);
                authTree.treeview('disableNode', [childNodes, { silent: true }]);

            },
        });
    }

    $scope.loadTable = function () {
        doGet($http, '/sys/queryRole', '', function (data) {
            console.log(data)
            $('#table').bootstrapTable('destroy').bootstrapTable({
                data: data.data
            });
        })
    }

    $scope.saveRole = function (type, valid) {
        if (valid) {
            var data = $scope.saveData
            var url = '/sys/insertRole'
            if (type == 'edit') {
                data = $scope.editData
                url = '/sys/updateRole'
            }
            doPost($http, url, data, function (data) {
                console.log(data)
                $('#addRole').modal('hide')
                $('#editRole').modal('hide')
                swal('操作成功', '', 'success');
                $scope.saveData = ''
                $scope.editData = ''
                $scope.loadTable()
            })
        } else {
            swal('验证不通过', '', 'warning')
        }
    }

    $scope.deleteRole = function (id) {
        doPost($http, '/sys/deleteRole', { _id: id }, function (data) {
            swal("删除成功!", "", "success");
            $scope.loadTable()
        })
    }

    $scope.saveRoleAuth = function (data) {
        var nodes = $('#authTree').treeview('getChecked');
        var authArr = []
        for (var i in nodes) {
            authArr.push(nodes[i].field)
        }
        console.log(authArr.toString())
        var data = {
            _id: data._id,
            dto: {
                power: authArr.toString()
            }
        }
        doPost($http, '/sys/updateRole', data, function (data) {
            console.log(data)
            $('#roleSetAuth').modal('hide')
            swal('操作成功', '', 'success');
            $scope.loadTable()
        })
    }


    $('#accountCreate').click(function () {
        $('#addRole').modal()
    })

    $scope.init();
})

NormalCMS.controller('sysAccountCtrl', function ($scope, $http) {
    
    
    
    $scope.roles=[{id: '0',name:'asdasd'}]  
    $scope.init = function () {
        $('#table').bootstrapTable('destroy').bootstrapTable({
        });
    }
        
        
          $scope.disableNode = function (nodes) {
        for (var j in nodes.nodes) {
            var state = nodes.nodes[j].state
            if (state) {
                nodes.nodes[j].state.disabled = true
            } else {
                nodes.nodes[j].state = {}
                nodes.nodes[j].state.disabled = true
            }

            for (var k in nodes.nodes[j].nodes) {
                var state2 = nodes.nodes[j].nodes[k].state
                if (state2) {
                    nodes.nodes[j].nodes[k].state.disabled = true
                } else {
                    nodes.nodes[j].nodes[k].state = {}
                    nodes.nodes[j].nodes[k].state.disabled = true
                }
            }
        }
    }
        
        
        
        
        
        $scope.showAuthList = function (row) {
        var role = row.power
        if (!role) {
            role = ""
        }
        role = role.split(',')
        $('#roleSetAuth').modal()
        var authTree = $('#authTree')
        authTree.children().remove()
        console.log(role)
        // var role = ['user', 'user-add', 'user-edit', 'home', 'home-find', 'project', 'project-gq', 'project-gq-add']

        var Auth = getAuths()

        for (var i in Auth) {
            if (role.indexOf(Auth[i].field) != -1) {
                if (Auth[i].state) {
                    Auth[i].state.checked = true
                } else {
                    Auth[i].state = {}
                    Auth[i].state.checked = true
                }
                for (var j in Auth[i].nodes) {
                    if (role.indexOf(Auth[i].nodes[j].field) != -1) {
                        if (Auth[i].nodes[j].state) {
                            Auth[i].nodes[j].state.checked = true
                        } else {
                            Auth[i].nodes[j].state = {}
                            Auth[i].nodes[j].state.checked = true
                        }
                        for (var k in Auth[i].nodes[j].nodes) {
                            if (role.indexOf(Auth[i].nodes[j].nodes[k].field) != -1) {
                                if (Auth[i].nodes[j].nodes[k].state) {
                                    Auth[i].nodes[j].nodes[k].state.checked = true
                                } else {
                                    Auth[i].nodes[j].nodes[k].state = {}
                                    Auth[i].nodes[j].nodes[k].state.checked = true
                                }
                            }
                        }
                    } else {
                        $scope.disableNode(Auth[i].nodes[j])
                    }
                }
            } else {
                $scope.disableNode(Auth[i])
            }
        }

        authTree.treeview({
            data: Auth,         // data is not optional
            levels: 5,
            showCheckbox: true,
            onNodeChecked: function (event, data) {
                //选中所有下级节点
                var childNodes = []
                for (var i in data.nodes) {
                    childNodes.push(data.nodes[i].nodeId)
                    for (var j in data.nodes[i].nodes) {
                        childNodes.push(data.nodes[i].nodes[j].nodeId)
                    }
                }
                console.log(childNodes)
                authTree.treeview('enableNode', [childNodes, { silent: true }]);
                authTree.treeview('checkNode', [childNodes, { silent: true }]);

            },
            onNodeUnchecked: function (event, data) {
                var childNodes = []
                for (var i in data.nodes) {
                    childNodes.push(data.nodes[i].nodeId)
                    for (var j in data.nodes[i].nodes) {
                        childNodes.push(data.nodes[i].nodes[j].nodeId)
                    }
                }
                authTree.treeview('uncheckNode', [childNodes, { silent: true }]);
                authTree.treeview('disableNode', [childNodes, { silent: true }]);

            },
        });
    }
        
        
        
        
            $scope.saveRoleAuth = function (data) {
        var nodes = $('#authTree').treeview('getChecked');
        var authArr = []
        for (var i in nodes) {
            authArr.push(nodes[i].field)
        }
        console.log(authArr.toString())
        var data = {
            _id: data._id,
            dto: {
                power: authArr.toString()
            }
        }
        doPost($http, '/sys/updateRole', data, function (data) {
            console.log(data)
            $('#roleSetAuth').modal('hide')
            swal('操作成功', '', 'success');
            $scope.loadTable()
        })
    }
        
        
       $('#accountCreate').click(function () {
        $('#addAccount').modal()
    })
    
    
    
    $scope.init();
})

NormalCMS.controller('sysAuthCtrl', function ($scope, $http) {
    $scope.init = function () {
        var Auth = getAuths()
        $('#authTreeShow').treeview({
            data: Auth,         // data is not optional
            levels: 5
        })
    }
    $scope.init()
})

